<?php

namespace app\admin\controller;

use app\admin\service\AuthRule;
use bxkj_module\controller\Auth;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Menu extends Controller
{
    public function index()
    {
        $homeInfo = [
            'title' => '首页',
            'href' => '/admin/index/set',
        ];
        $logoInfo = [
            'title' => '秉信小说',
            'image' => '/public/images/newLogo.png',
        ];
        /* $menuInfo = Cache::store('redis')->get('cache:menuList');
         if (empty($menuInfo)) {
             $menuInfo = $this->getMenuList();
             Cache::store('redis')->set('cache:menuList', json_encode($menuInfo));
         } else {
             $menuInfo = json_decode($menuInfo, true);
         }*/
        $menuInfo = $this->getMenuList();
        $systemInit = [
            'homeInfo' => $homeInfo,
            'logoInfo' => $logoInfo,
            'menuInfo' => $menuInfo,
        ];
        return json($systemInit);
    }

    public function getlist()
    {
        if (Request::isAjax()) {
            $authRuleSevice = new AuthRule();
            $list = $authRuleSevice->getList();
            return $this->success(lang('SUCCESS'), $list);
        } else {
            return View::fetch();
        }
    }

    public function add()
    {
        $authRuleSevice = new AuthRule();
        if (Request::isAjax()) {
            $post = input();
            $res = $authRuleSevice->add($post);
            if (!$res) $this->error($authRuleSevice->getError());
            return $this->success(lang('ADD_SUCCESS'));
        } else {
            $pid = !empty(input('pid')) ? input('pid') : 1;
            $parent = $authRuleSevice->getOne(['id' => $pid]);
            if (empty($parent)) return $this->error(lang('UP_MENU_NOT_ENPTY'));
            View::assign('parent', $parent);
            return View::fetch();
        }
    }

    public function edit()
    {
        $authRuleSevice = new AuthRule();
        if (Request::isAjax()) {
            $post = input();
            $res = $authRuleSevice->edit($post);
            if (!$res) $this->error($authRuleSevice->getError());
            return $this->success(lang('EDIT_SUCCESS'));
        } else {
            $id = input('id');
            if (empty($id)) return $this->error(lang('ERROR'));
            $_info = $authRuleSevice->getOne(['id' => $id]);
            if (empty($_info)) return $this->error(lang('NO_MENU'));
            $parent = $authRuleSevice->getOne(['id' => $_info['pid']]);
            View::assign('_info', $_info);
            View::assign('parent', $parent);
            return View::fetch('add');
        }
    }

    public function del()
    {
        if (Request::isAjax()) {
            $ids = input('id');
            $authRuleSevice = new AuthRule();
            if (empty($ids)) return $this->error(lang('ERROR'));
            $res = $authRuleSevice->del($ids);
            if (!$res) $this->error($authRuleSevice->getError());
            return $this->success(lang('DELETE_SUCCESS'));
        }
    }

    public function getMenuList()
    {
        $auth = new Auth();
        $menuList = Db::name('auth_rule')
            ->field('id, pid, title, name as href, icon, target')
            ->order('sort', 'desc')
            ->where(['status' => 1, 'is_show' => 1])
            ->select();
        $menuList = $this->buildMenuChild(0, $menuList, $auth);
        return $menuList;
    }

    private function buildMenuChild($pid, $menuList, Auth $auth)
    {
        $treeList = [];
        foreach ($menuList as $v) {
            if ($pid == $v['pid']) {
                $node = $v;
                $child = $this->buildMenuChild($v['id'], $menuList, $auth);
                if (!empty($child)) {
                    $node['child'] = $child;
                }
                if (!$auth->check($v['href'], session($this->sysName)['admin_id']) && empty(session($this->sysName)['is_admin'])) {
                    unset($node);
                } else {
                    $treeList[] = $node;
                }
                // $treeList[] = $node;
            }
        }
        return $treeList;
    }
}